﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nivdal.AuxCode;
using System.Threading;
using System.Globalization;

namespace Nivdal
{
    public partial class Shop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();

            if (!IsPostBack)
            {
                if (!HttpContext.Current.User.Identity.IsAuthenticated)
                    Response.Redirect("~/Default.aspx");
                if (Request.QueryString["TournamentId"] != null)
                {
                    string TournamentId = Request.QueryString["TournamentId"];
                    string TournamentName = GND.ReadNivdalStringData("TournamentId", "Nivdal_GetTournamentNameById", TournamentId.ToString());
                    PayTypeRadioButtonList.SelectedIndex = 2;
                    TournamentIdTextBox.Text = TournamentId;
                    this.custom.Value = "2," + TournamentId + "," + ou.UserId;
                    MessageLabel.Text = Resources.Resource.Shop_YouArePayingFor + " " + TournamentName;
                }
                else
                {
                    this.custom.Value = "0," + ou.UserId + "," + ou.UserId;
                    PayTypeRadioButtonList.SelectedIndex = 0;
                }

                this.PremiumInfoDS.SelectParameters.Add("UserId", ou.UserId);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "HideControl();", true); 
            }
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            this.CurrentUserIdHidden.Value = ou.UserId;
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.Shop_PageTitle;
            this.PayForMySelfHidden.Value = Resources.Resource.Shop_YouArePayingForYourself;
            this.PayForAnotherTeamHidden.Value = Resources.Resource.Shop_YouArePayingFor;            
        }

        protected void SetButton_Click(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
          
            string FriendUserId = GND.ReadNivdalStringData("TeamId", "Nivdal_GetUserIdByTeamId", TeamIdTextBox.Text.Trim());
            this.MessageLabel.Text = PayForAnotherTeamHidden.Value.ToString() + " " + GND.ReadNivdalStringData("UserId", "Nivdal_GetUserNameById", FriendUserId);
            this.custom.Value = "1," + FriendUserId + "," + this.CurrentUserIdHidden.Value;
               
        }

        protected void SetTournamentButtonClick(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();

            int TournamentId = 0;
            if (!int.TryParse(TournamentIdTextBox.Text.Trim(), out TournamentId))
                MessageLabel.Text = Resources.Resource.Shop_IncorrectTournamentNumber;
            else
            {
                string TournamentName = GND.ReadNivdalStringData("TournamentId", "Nivdal_GetTournamentNameById", TournamentId.ToString());
                
                if (TournamentName.Length > 0)
                {
                    bool CanPay = GND.ReadNivdalBoolData("TournamentId", "UserId", "Nivdal_CanUserPayForTournament", TournamentId, ou.UserId);
                    
                    if (CanPay)
                    {
                        MessageLabel.Text = Resources.Resource.Shop_YouArePayingFor + " " + TournamentName;
                        custom.Value = "2," + TournamentId.ToString() + "," + this.CurrentUserIdHidden.Value;
                    }
                    else
                        MessageLabel.Text = Resources.Resource.Shop_CannotPayForNationalTournament;
                }
                else
                    MessageLabel.Text = Resources.Resource.Shop_IncorrectTournamentNumber;
            }
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}